package org.ant.gui.panes;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.ant.ctrl.CodesManager;
import org.ant.gui.layout.GBC;

public class CodePane extends JPanel implements ActionListener {

	private JTextArea textArea;

	private String path;

	public CodePane(String path) {
		super();
		this.path = path;
		setLayout(new GridBagLayout());
		loadComponent();
	}

	protected void loadComponent() {
		add(new JScrollPane(getTextArea()), new GBC(0, 0, 1, 1).setWeight(1, 1)
				.setFill(GBC.BOTH));
	}

	public String getName() {
		String[] s = path.split("/");
		System.out.println(s[s.length - 1]);
		return s[s.length - 1];
	}

	public String getPath() {
		return path;
	}

	public JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			try {
				FileReader fr = new FileReader(path);
				StringBuffer temp = new StringBuffer();
				while (fr.ready()) {
					temp.append((char)fr.read());
				}
				textArea.setText(temp.toString());
				fr.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return textArea;
	}


	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof CodesManager) {

		}
	}
}
